package repository

import (
	v1 "SmartLibrary/api/v1"
	"SmartLibrary/internal/model"
	"context"
)

type BookAdminRepository interface {
	LoginBookAdmin(ctx context.Context, username string) (*model.BookAdmin, error)
	//GetBookAdmin 返回管理员信息
	GetBookAdmin(ctx context.Context, id *string) (*model.BookAdmin, error)
	// CheckCardNumber 检查借阅号是否正确
	CheckCardNumber(ctx context.Context, cardNumber int64) (*model.User, error)
	// GetRule 查询借阅规则接口
	GetRule(ctx context.Context, ruleNumber int32) (*model.TBookBorrowRule, error)
	// GetBookBorrow 统计借阅的书的所有信息
	GetBookBorrow(ctx context.Context, cardNumber int64) (*[]model.TBooksBorrow, error)
	// CreateBookBorrow 创建借阅信息
	CreateBookBorrow(ctx context.Context, req *model.TBooksBorrow) error
	// GetBookMessage 获取图书信息
	GetBookMessage(ctx context.Context, column string, message interface{}) ([]model.TBooksBorrow, error)
	// ReturnBook 归还图书
	ReturnBook(ctx context.Context, req *v1.BookReturnRequest) error
	// CreateViolation 创建违章信息
	CreateViolation(ctx context.Context, violation *model.TViolation) error
	// FindViolation 查询违章信息
	FindViolation(ctx context.Context, message *v1.BorrowReportRequest) (*[]model.TViolation, error)
	// GetBookAdminName 通过管理员id查询名字
	GetBookAdminName(ctx context.Context, id int64) (string, error)
	// ReturnReport 获取还书报表
	ReturnReport(ctx context.Context, req *v1.ReturnReportRequest) (*[]model.TBooksBorrow, error)
	// GetNotice 获取公告信息
	GetNotice(ctx context.Context, req *v1.GetBookMessageRequest) (*[]model.Notice, error)
	// CreateNotice 添加新公告
	CreateNotice(ctx context.Context, notice *model.Notice) error
	// DeleteNotice 删除公告
	DeleteNotice(ctx context.Context, id int64) error
	// GetNoticeById 获取指定id 的公告
	GetNoticeById(ctx context.Context, id int64) (*model.Notice, error)
	// UpdateNotice 修改已更改的公告
	UpdateNotice(ctx context.Context, notice *v1.UpdateNoticeRequest) error
	//获取借阅表中的所有信息
	GetAllBookBorrow(ctx context.Context) (int, error)
	//获取所有的还书信息
	GetAllReturnBook(ctx context.Context) (int, error)
	//获取所有公告信息
	GetAllNotice(ctx context.Context) (int, error)
}

func NewBookAdminRepository(
	repository *Repository,
) BookAdminRepository {
	return &bookAdminRepository{
		Repository: repository,
	}
}

type bookAdminRepository struct {
	*Repository
}

//	func (r *bookAdminRepository) GetBookAdmin(ctx context.Context, id int64) (*model.BookAdmin, error) {
//		var bookAdmin model.BookAdmin
//
//		return &bookAdmin, nil
//	}
func (r *bookAdminRepository) LoginBookAdmin(ctx context.Context, username string) (*model.BookAdmin, error) {
	var req *model.BookAdmin
	if err := r.DB(ctx).Where("username=?", username).First(&req).Error; err != nil {
		return nil, err
	}
	return req, nil
}
func (r *bookAdminRepository) GetBookAdmin(ctx context.Context, id *string) (*model.BookAdmin, error) {
	var req *model.BookAdmin
	if err := r.DB(ctx).Where("book_admin_id=?", id).First(&req).Error; err != nil {
		return nil, err
	}
	return req, nil
}
func (r *bookAdminRepository) CheckCardNumber(ctx context.Context, cardNumber int64) (*model.User, error) {
	var req *model.User
	if err := r.DB(ctx).Where("card_number=?", cardNumber).First(&req).Error; err != nil {
		return nil, err
	}
	return req, nil
}
func (r *bookAdminRepository) GetRule(ctx context.Context, ruleNumber int32) (*model.TBookBorrowRule, error) {
	var req *model.TBookBorrowRule
	if err := r.DB(ctx).Where("book_rule_id=?", ruleNumber).First(&req).Error; err != nil {
		return nil, err
	}
	return req, nil
}

func (r *bookAdminRepository) GetBookBorrow(ctx context.Context, cardNumber int64) (*[]model.TBooksBorrow, error) {
	var req *[]model.TBooksBorrow
	if err := r.DB(ctx).Where("card_number=?", cardNumber).Find(&req).Error; err != nil {
		return nil, err
	}
	return req, nil
}

func (r *bookAdminRepository) CreateBookBorrow(ctx context.Context, req *model.TBooksBorrow) error {
	return r.DB(ctx).Create(req).Error
}
func (r *bookAdminRepository) GetBookMessage(ctx context.Context, column string, message interface{}) ([]model.TBooksBorrow, error) {
	//查询所有满足的数据
	var req []model.TBooksBorrow
	if err := r.DB(ctx).Where(column+"=?", message).Find(&req).Error; err != nil {
		return nil, err
	}
	return req, nil
}
func (r *bookAdminRepository) ReturnBook(ctx context.Context, req *v1.BookReturnRequest) error {
	//更新数据库表
	return r.DB(ctx).Model(&model.TBooksBorrow{}).Where("book_number=?", req.BookNumber).Update("return_date", req.ReturnDate).Error
}

func (r *bookAdminRepository) CreateViolation(ctx context.Context, violation *model.TViolation) error {
	return r.DB(ctx).Create(violation).Error
}

func (r *bookAdminRepository) FindViolation(ctx context.Context, message *v1.BorrowReportRequest) (*[]model.TViolation, error) {
	var req *[]model.TViolation
	//模糊查询
	if len(message.Condition) == 0 || len(message.Query) == 0 {
		if err := r.DB(ctx).Offset((message.PageNum - 1) * message.Size).Limit(message.Size).Find(&req).Error; err != nil {
			return nil, err
		}
	} else {
		if err := r.DB(ctx).Where("? LIKE ?", message.Condition, "%"+message.Query+"%").Offset((message.PageNum - 1) * message.Size).Limit(message.Size).Find(&req).Error; err != nil {
			return nil, err
		}
	}
	return req, nil
}

func (r *bookAdminRepository) GetBookAdminName(ctx context.Context, id int64) (string, error) {
	var req *model.BookAdmin
	if err := r.DB(ctx).Where("book_admin_id=?", id).First(&req).Error; err != nil {
		return "", err
	}
	return req.Username, nil
}
func (r *bookAdminRepository) ReturnReport(ctx context.Context, message *v1.ReturnReportRequest) (*[]model.TBooksBorrow, error) {
	var req *[]model.TBooksBorrow
	if len(message.Condition) == 0 || len(message.Query) == 0 {
		//全表查询
		if err := r.DB(ctx).Where("return_date = '2001-01-01 08:00:00'").Offset((message.PageNum - 1) * message.Size).Limit(message.Size).Find(&req).Error; err != nil {
			return nil, err
		}
	} else {
		if err := r.DB(ctx).Where("? LIKE ? and return_date = '2001-01-01 08:00:00'", message.Condition, "%"+message.Query+"%").Offset((message.PageNum - 1) * message.Size).Limit(message.Size).Find(&req).Error; err != nil {
			return nil, err
		}
	}
	return req, nil
}
func (r *bookAdminRepository) GetNotice(ctx context.Context, message *v1.GetBookMessageRequest) (*[]model.Notice, error) {
	var req *[]model.Notice
	if err := r.DB(ctx).Offset((message.PageNum - 1) * message.PageSize).Limit(message.PageSize).Find(&req).Error; err != nil {
		return nil, err
	}
	return req, nil
}

func (r *bookAdminRepository) CreateNotice(ctx context.Context, notice *model.Notice) error {
	return r.DB(ctx).Create(notice).Error
}

func (r *bookAdminRepository) DeleteNotice(ctx context.Context, id int64) error {
	return r.DB(ctx).Where("notice_id=?", id).Delete(&model.Notice{}).Error
}

func (r *bookAdminRepository) GetNoticeById(ctx context.Context, id int64) (*model.Notice, error) {
	var req *model.Notice
	if err := r.DB(ctx).Where("notice_id=?", id).First(&req).Error; err != nil {
		return nil, err
	}
	return req, nil
}

// UpdateNotice 修改已更改的公告
func (r *bookAdminRepository) UpdateNotice(ctx context.Context, notice *v1.UpdateNoticeRequest) error {
	return r.DB(ctx).Model(&model.Notice{}).Where("notice_id=?", notice.NoticeId).Updates(model.Notice{
		NoticeTitle:   notice.NoticeTitle,
		NoticeContent: notice.NoticeContent,
	}).Error
}

func (r *bookAdminRepository) GetAllBookBorrow(ctx context.Context) (int, error) {
	var req *[]model.TViolation
	if err := r.DB(ctx).Find(&req).Error; err != nil {
		return 0, err
	}
	return len(*req), nil
}

func (r *bookAdminRepository) GetAllReturnBook(ctx context.Context) (int, error) {
	var req *[]model.TBooksBorrow
	if err := r.DB(ctx).Where("return_date = '2001-01-01 08:00:00'").Find(&req).Error; err != nil {
		return 0, err
	}
	return len(*req), nil
}

func (r *bookAdminRepository) GetAllNotice(ctx context.Context) (int, error) {
	var req *[]model.Notice
	if err := r.DB(ctx).Find(&req).Error; err != nil {
		return 0, err
	}
	return len(*req), nil
}
